function setEventRangeIndexToOne() {
upDateEventRange(-99999);
var upperEventIntervalBound = currentEventRangeIndex + 9;
var eventRangeString = currentEventRangeIndex + " - " + upperEventIntervalBound;		
document.getElementById("eventRange").innerHTML = eventRangeString;
}

function sortByDistance_wrapper(event) { 
upDateEventRange(-99999);
$('.sortingButton').css({ "color":"white"});
$('#closestButton').css({ "color": "#777777"});
//alert("Sorting events by shortest distance away");
markerData.sort(sortByDistance);
populateWithEvents();
}


function distanceLatLong(lat1, long1, lat2, long2) {
    return Math.sqrt( (lat1-lat2)*(lat1-lat2) + (long1-long2)*(long1-long2) );
}


function sortByDistance(a, b) {
    //Get distance from current location to event
    distA = distanceLatLong(currentLat, currentLon, a[0], a[1]);
    distB = distanceLatLong(currentLat, currentLon, b[0], b[1]);
    return (distA - distB);
}





function sortByTime_wrapper(event) { 
upDateEventRange(-99999);
$('.sortingButton').css({ "color":"white"});
$('#soonestButton').css({ "color": "#777777"});
//alert("Sorting events by soonest start time");
markerData.sort(sortByTime);
populateWithEvents();
}



function sortByTime(a, b){
    //Get Start times of event array
    one = a[5];
    two = b[5];
    if(one.getHours() < two.getHours()) return -1;
    if(two.getHours() < one.getHours()) return 1;
    else {
        if(one.getMinutes() < two.getMinutes()) return -1;
        if(two.getMinutes() < one.getMinutes()) return 1;
        else return 0;
    }
}




function sortByRecommended_wrapper(event) { 
upDateEventRange(-99999);
//alert("Sorting events by recommendation");
$('.sortingButton').css({ "color":"white"});
$('#recommendedButton').css({ "color": "#777777"});
markerData.sort(sortByPreference);
populateWithEvents();
}

function getRating(host) {
	var x = 0;
	for (var i=0;i<preferences.length;i++){
		if(preferences[i][0] == host) {
		x = i;
		} 
	}
	return preferences[x][1];
}

function sortByPreference(a, b) {
	//alert("sdlfkj");
	//retrieve hostnames from events
	hostA = a[4];
	hostB = b[4];
	//alert(hostA + ", " + hostB);
	//retrieve ratings from preferences array
	rateA = getRating(hostA);
	rateB = getRating(hostB);
	return(rateB - rateA);
}